package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.DoctorDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchDoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DoctorServiceImpl implements IDoctorService {

    @Autowired
    private DoctorDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<DoctorInfo> selectPageListByQueryGetMap(Page<DoctorInfo> page, SearchDoctorInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getDoctorName())) {
            params.put("doctorName", searchInfo.getDoctorName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctorPhone())) {
            params.put("doctorPhone", searchInfo.getDoctorPhone());
        }
        return dao.getQueryPage(page, params);
    }

    public DoctorInfo selectOneById(String doctorId) {
        String sql = "select * from doctor_info where id= '" + doctorId + "'";
        DoctorInfo info = sqlMapper.selectOne(sql, DoctorInfo.class);
        return info;
    }

    public int insert(DoctorInfo info) {
        String id = CommUtils.getUUID();
        String sql = "";
        if (info.getDoctorBirthday().length() == 0) {
            sql = "INSERT INTO `doctor_info` (`id`, `doctor_name`, `doctor_phone`, `doctor_sex`, `doctor_birthday`, `remarks`) VALUES" +
                    "('" + id + "','" + info.getDoctorName().trim() + "','" + info.getDoctorPhone().trim() + "'," + info.getDoctorSex() + "," + null + ",'" + info.getRemarks().trim() + "');";
        } else {
            sql = "INSERT INTO `doctor_info` (`id`, `doctor_name`, `doctor_phone`, `doctor_sex`, `doctor_birthday`, `remarks`) VALUES" +
                    "('" + id + "','" + info.getDoctorName().trim() + "','" + info.getDoctorPhone().trim() + "'," + info.getDoctorSex() + ",'" + info.getDoctorBirthday() + "','" + info.getRemarks().trim() + "');";
        }

        return sqlMapper.insert(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from doctor_info where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(DoctorInfo info) {
        if  (info.getDoctorBirthday().length() == 0) {
            info.setDoctorBirthday(null);
            dao.updateById(info);
            String sql = "update doctor_info set doctor_birthday="+null+" where id = '" + info.getId() + "';";
            return sqlMapper.update(sql);
        }else{
            return dao.updateById(info);
        }
    }

    public List<Map<String, Object>> list() {
        String sql = "select id, doctor_name from doctor_info;";
        return sqlMapper.selectList(sql);
    }
}
